package com.manager.cms.controller;

import com.manager.cms.bo.ExpressDelivery;
import com.manager.cms.bo.ExpressMan;
import com.manager.cms.common.CommonResponse;
import com.manager.cms.common.Pageable;
import com.manager.cms.service.ExpressDeliveryService;
import com.manager.cms.service.ExpressManService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 
 *
 * @author 
 * @version v1.0
 * @date 2020-05-19
 */
@Slf4j
@RestController
@RequestMapping(value = "/expressDelivery")
public class ExpressDeliveryController {

    @Autowired
    private ExpressDeliveryService expressdeliveryservice;

    @Autowired
    private ExpressManService expressManService;

    // 用户下单
    @PostMapping("/save")
    public CommonResponse save(HttpServletRequest request, HttpServletResponse response, ExpressDelivery record) {
        try {
            record.setId(UUID.randomUUID().toString());
            record.setCreateDate(new Date());
            record.setModifyDate(new Date());
            String token = request.getHeader("token");
            record.setSendUserId(token);

            // 分配快递员接单
            List<ExpressMan> list = expressManService.getList(new ExpressMan());
            if(list.size() == 0){
                return CommonResponse.fail("没有快递员接单");
            }
            Collections.shuffle(list);
            ExpressMan expressMan = list.get(0);

            record.setExpressManId(expressMan.getId());
            record.setOrderStatus(2);
            record.setIsReceive(true);
            record.setReceiveDate(new Date());

            expressdeliveryservice.insert(record);
            return CommonResponse.succeed();
        } catch (Exception e) {
            log.error("保存失败，{}", e);
        }
        return CommonResponse.fail();
    }

    // 用户修改
    // 管理员修改
    // 快递员修改
    @PostMapping("/update")
    public CommonResponse update(HttpServletRequest request, HttpServletResponse response,ExpressDelivery record) {
        try {
            record.setModifyDate(new Date());
            expressdeliveryservice.update(record);
            return CommonResponse.succeed();
        } catch (Exception e) {
            log.error("保存失败，{}", e);
        }
        return CommonResponse.fail();
    }

    // 用户删除
    // 管理员删除
    @PostMapping("/delete")
    public CommonResponse del(HttpServletRequest request, HttpServletResponse response, String id) {
        try {
            if (id == null) {
                return CommonResponse.fail("id不能为空");
            }
            expressdeliveryservice.del(id);
            return CommonResponse.succeed();
        } catch (Exception e) {
            log.error("删除失败，{}", e);
        }
        return CommonResponse.fail();
    }

    @GetMapping("/selectByPrimaryKey")
    public CommonResponse selectByPrimaryKey(HttpServletRequest request, HttpServletResponse response, String id) {
        try {
            if (id == null) {
                return CommonResponse.fail("id不能为空");
            }
            ExpressDelivery info = expressdeliveryservice.selectByPrimaryKey(id);
            return CommonResponse.succeed(info);
        } catch (Exception e) {
            log.error("查询失败，{}", e);
        }
        return CommonResponse.fail();
    }

    @GetMapping("/list")
    public CommonResponse getList(HttpServletRequest request, HttpServletResponse response, ExpressDelivery record) {
        try {
            List<ExpressDelivery> list = expressdeliveryservice.getList(record);
            return CommonResponse.succeed(list);
        } catch (Exception e) {
            log.error("查询失败，{}", e);
        }
        return CommonResponse.fail();
    }

    @GetMapping("/listByUser")
    public CommonResponse listByUser(HttpServletRequest request, HttpServletResponse response, ExpressDelivery record) {
        try {
            String token = request.getHeader("token");
            record.setSendUserId(token);
            List<ExpressDelivery> list = expressdeliveryservice.getList(record);
            return CommonResponse.succeed(list);
        } catch (Exception e) {
            log.error("查询失败，{}", e);
        }
        return CommonResponse.fail();
    }

    // 用户查询
    @GetMapping("/pageListByUser")
    public CommonResponse pageListByUser(HttpServletRequest request, HttpServletResponse response,ExpressDelivery record,
                                   @RequestParam(value="page",defaultValue = "1")Integer page,
                                   @RequestParam(value="limit",defaultValue = "10")Integer limit) {
        try {
            String token = request.getHeader("token");
            record.setSendUserId(token);
            Pageable<ExpressDelivery> pageable = expressdeliveryservice.pageList(record,page,limit);
            return CommonResponse.succeed(pageable);
        } catch (Exception e) {
            log.error("查询失败，{}", e);
        }
        return CommonResponse.fail();
    }

    // 快递员查询
    @GetMapping("/pageListByExpress")
    public CommonResponse pageListByExpress(HttpServletRequest request, HttpServletResponse response,ExpressDelivery record,
                                         @RequestParam(value="page",defaultValue = "1")Integer page,
                                         @RequestParam(value="limit",defaultValue = "10")Integer limit) {
        try {
            String token = request.getHeader("token");
            record.setExpressManId(token);
            Pageable<ExpressDelivery> pageable = expressdeliveryservice.pageList(record,page,limit);
            return CommonResponse.succeed(pageable);
        } catch (Exception e) {
            log.error("查询失败，{}", e);
        }
        return CommonResponse.fail();
    }

    // 管理员查询
    @GetMapping("/pageList")
    public CommonResponse pageList(HttpServletRequest request, HttpServletResponse response,ExpressDelivery record,
                                   @RequestParam(value="page",defaultValue = "1")Integer page,
                                   @RequestParam(value="limit",defaultValue = "10")Integer limit) {
        try {
            Pageable<ExpressDelivery> pageable = expressdeliveryservice.pageList(record,page,limit);
            return CommonResponse.succeed(pageable);
        } catch (Exception e) {
            log.error("查询失败，{}", e);
        }
        return CommonResponse.fail();
    }
}